import service from "@/api/service";
export default {
  namespaced: true,
  state: {
    userStatus: [
      {value:"Disabled", label:"禁用"},
      {value:"Unverified", label:"未验证"},
      {value:"Locked", label:"锁定"},
      {value:"Interim", label:"临时"},
      {value:"Standard", label:"标准"}
    ],
    queryObject: {
      pageIndex: 1,
      pageSize: 20,
      sortFields: {
        "UserID": true,
      },
      parameters: {
        keyword: null,
        userStatus: null,
      },
    },
    dataTable: {
      result: [],
      pageIndex: 1,
      pageSize: 20,
      pageCount: 0,
      rowCount: 0,
    },
    currentItem: {
      userId: null,
      displayName: null,
      loginName: null,
      secEmail: null,
      cellphone: null,
      userStatus: null,
      createTime: null,
      updateTime: null,
      roles: [],
    },
  },
  mutations: {
    setData(state, dataTable) {
      state.dataTable = dataTable;
    },
    selectItem(state, item) {
      state.currentItem = item;
    },
    setParameters(state, parameters) {
      for (let name in parameters) {
        state.queryObject.parameters[name] = parameters[name];
      }
    },
  },
  actions: {
    query({ commit, state }) {
      return service.user
        .query({ data: state.queryObject })
        .then((response) => {
          commit("setData", response.success ? response.data : null);
          return response;
        })
        .catch((error) => {
          return error;
        });
    },
    item({ commit, state }, id) {
      return service.user
        .item({ params: { id } })
        .then((response) => {
          return response;
        })
        .catch((error) => {
          return error;
        });
    },
    create({ commit, state, dispatch }, dto) {
      return service.user
        .create({ data: dto })
        .then((response) => {
          if (response.success) dispatch("query");
          return response;
        })
        .catch((error) => {
          return error;
        });
    },
    update({ commit, state, dispatch }, dto) {
      return service.user
        .update({ data: dto })
        .then((response) => {
          if (response.success) dispatch("query");
          return response;
        })
        .catch((error) => {
          return error;
        });
    },
    // delete({ commit, state, dispatch }, id){
    //   return service.user.delete({data:id}).then(response => {
    //     if (response.success)
    //       dispatch('query');
    //     return response;
    //   }).catch(error => {
    //       return error;
    //   });
    // }
    initPwd({ commit, state }, id) {
      return service.user
        .initPwd({ params: { id } })
        .then((response) => {
          if (response.success) alert("操作成功","消息","success");
          return response;
        })
        .catch((error) => {
          return error;
        });
    },
  },
};
